//
//  CZUserViewModel.swift
//  Weibo24
//
//  Created by apple on 16/2/26.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
/*
    MVVM
    1. View 和控制器结合, 
    2. View 和控制器不能直接和Model进行交互,需要靠ViewModel
    3. ViewModel,网络请求, Model,其他的逻辑判断

*/
class CZUserViewModel: NSObject {

    
    // Swift里的单例
    static let shareInstance = CZUserViewModel()
    
    // 给这个model 赋值
    var model: CZUserModel?
    
    // access_token,进行使用
    // 我实现了属性的 get 方法
    // get方法的属性 -> 计算型属性
    var access_token: String? {
        
//        get {
//            return model?.access_token
//        }
        return model?.access_token
        
    }
    
//    func getAccessToken() -> String? {
//        return model?.access_token
//    }
    
    //定义一个属性,判断是否登录
    var isLogin: Bool? {
        
        return model?.access_token != nil ? true : false
        
    }
   override init() {
        model = CZUserModel.readObject("info.aaaaa")
    }
    
    
    //MARK:--通过code来换取Token
    // 定义一个请求Token的方法
    //添加2个闭包参数,一个成功,一个失败
    func loadToken(code: String,success: ()->(),failure: ()->()) {
        
        
        let params = ["client_id": App_KEY,
            "client_secret":APP_SECERT,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":App_URL
        ]
        
       CZHTTPClient.shareInstance.RequestData(.POST, URLString: "https://api.weibo.com/oauth2/access_token", parameters: params, success: { (JSON) -> () in
        // 网络访问成功之后,走这里
        
        if let Odic = JSON as? [String: NSObject] {
            // 强制解包会崩溃
            let userModel = CZUserModel(dic: Odic)
            
            print(userModel.access_token)
            
            
            self.getPersonInfo(userModel, success: success, failure: failure)
        }

        }) { (error) -> () in
            //失败也得处理
            //回调
            failure()
        }
        
        
    }
    
    
    //MARK:--通过Toekn来获取用户信息
    func getPersonInfo(userModel: CZUserModel,success: ()->(),failure: ()->()){
        
        
        let params = ["access_token":userModel.access_token ?? "",
            "uid":userModel.uid ?? ""
        ]
        
        
        print(params)// ["access_token": "2.00TB2V3GjxjoGBcbc730ac43tQwj3C", "uid": "5829345367"]
        
        CZHTTPClient.shareInstance.RequestData(.GET, URLString: "https://api.weibo.com/2/users/show.json", parameters: params, success: { (JSON) -> () in
            if let dic = JSON as? [String: NSObject]{
                // userModel.profile_image_url是 String类型的
                // dic["profile_image_url"] 是NSObject
                userModel.profile_image_url = dic["profile_image_url"] as? String
                
                userModel.screen_name = dic["screen_name"] as? String
                
                
                userModel.save("info.aaaaa")
                //成功的回调
                success()
            }
            }) { (error) -> () in
                print(error)
                //失败也得处理
                failure()
        }
        
        
    
        
    }

    
}
